//package com.redlight.filter;//package com.redlight.filter;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.redlight.base.RestResult;
//import com.redlight.base.ResultStatus;
//import com.redlight.pool.ThreadPool;
//import com.redlight.pool.ThreadPoolFactory;
//import com.redlight.token.Audience;
//
//import java.io.IOException;
//import javax.annotation.Resource;
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import com.redlight.token.JwtHelper;
//import com.redlight.utils.StringUtil;
//import io.jsonwebtoken.Claims;
//import org.springframework.stereotype.Component;
//import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.context.support.SpringBeanAutowiringSupport;
//
///**
// * @author chao
// * @Title: ApiAuthFilter
// * @date 2019-01-24 21:30
// */
//
//@Component
//@WebFilter({"/api/*"})
//public class ApiAuthFilter implements Filter {
//    private Audience audience = Audience.getInstance();
//    @Resource
//    private ThreadPoolFactory threadPoolFactory;
//    public ApiAuthFilter() {
//    }
//    @Override
//    public void init(FilterConfig filterConfig) {
//        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, filterConfig.getServletContext());
//    }
//    @Override
//    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//        System.out.println("do filter~~~~~~~~~~~~~~~~~~~~~~~~~");
//        HttpServletRequest httpRequest = (HttpServletRequest)request;
//        HttpServletResponse httpResponse = (HttpServletResponse)response;
//        if (httpRequest.getMethod().equals(RequestMethod.OPTIONS.name())) {
//            httpResponse.setCharacterEncoding("UTF-8");
//            httpResponse.setContentType("application/json; charset=utf-8");
//            httpResponse.setHeader("Access-Control-Allow-Origin", "*");
//            httpResponse.setHeader("Access-Control-Allow-Methods", "POST, GET, DELETE");
//            httpResponse.setHeader("Access-Control-Allow-Headers", "Accept,Authorization,Content-Type");
//            httpResponse.setHeader("Access-Control-Max-Age", "3600");
//            httpResponse.setStatus(200);
//
//            chain.doFilter(request, response);
//        } else {
//            httpResponse.setCharacterEncoding("UTF-8");
//            httpResponse.setContentType("application/json; charset=utf-8");
//            httpResponse.setHeader("Access-Control-Allow-Origin", "*");
//            httpResponse.setHeader("Access-Control-Allow-Methods", "POST, GET, DELETE");
//            httpResponse.setHeader("Access-Control-Allow-Headers", "Accept,Authorization,Content-Type");
//            httpResponse.setHeader("Access-Control-Max-Age", "3600");
//            httpResponse.setStatus(200);
//
//            if (!this.audience.isAuthenticate()) {
//                chain.doFilter(request, response);
//            } else {
//                String userId = httpRequest.getParameter("userId");
//                if (StringUtil.isNotBlank(userId)) {
//                    httpRequest.getSession().setAttribute("SubjectId", userId);
//                    chain.doFilter(request, response);
//                } else {
//                    String Authorization = httpRequest.getHeader("Authorization");
//                    String tokenType = this.audience.getTokenType();
//                    if (Authorization != null && Authorization.length() > tokenType.length() && Authorization.startsWith(tokenType + " ")) {
//                        String auth = Authorization.substring(tokenType.length() + 1);
//                        Claims claims = JwtHelper.parseJWT(auth, this.audience.getBase64Secret());
//                        if (claims != null) {
//                            userId = claims.getId();
//                            httpRequest.getSession().setAttribute("SubjectId", userId);
//                            httpRequest.getSession().setAttribute("Authorization", Authorization);
//                            chain.doFilter(request, response);
//                            return;
//                        }
//                    }
//
//                    ObjectMapper mapper = new ObjectMapper();
//                    RestResult result = new RestResult(ResultStatus.INVALID_TOKEN);
//                    httpResponse.getWriter().write(mapper.writeValueAsString(result));
//                }
//            }
//        }
//    }
//
//    public void destroy() {
//    }
//}
